#include "cpup_demo.h"

#include "los_task.h"
#include "los_cpup.h"

#define TEST_TASK_PRIO  5
#define TASK_DELAY_TIME 100
VOID CpupTask(VOID)
{
    printf("entry cpup test example\n");
    usleep(TASK_DELAY_TIME);
    usleep(TASK_DELAY_TIME);
    printf("exit cpup test example\n");
}

UINT32 cpup_entry(VOID)
{
    UINT32 ret;
    UINT32 cpupUse;
    UINT32 taskID;
    TSK_INIT_PARAM_S cpupTestTask = { 0 };

    cpupTestTask.pfnTaskEntry = (TSK_ENTRY_FUNC)CpupTask;
    cpupTestTask.pcName       = "TestCpupTsk";
    cpupTestTask.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    cpupTestTask.usTaskPrio   = TEST_TASK_PRIO;
    ret = LOS_TaskCreate(&taskID, &cpupTestTask);
    if(ret != LOS_OK) {
        printf("cpupTestTask create failed .\n");
        return LOS_NOK;
    }

    usleep(TASK_DELAY_TIME);

    /* 获取当前系统CPU占用率 */
    cpupUse = LOS_SysCpuUsage();
    printf("the current system cpu usage is: %u.%u\n",
            cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);

    /* 获取当前系统历史CPU占用率 */
    cpupUse = LOS_HistorySysCpuUsage(CPUP_LESS_THAN_1S);
    printf("the history system cpu usage in all time：%u.%u\n",
           cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);

    /* 获取指定任务的CPU占用率 */
    cpupUse = LOS_TaskCpuUsage(taskID);
    printf("cpu usage of the cpupTestTask:\n TaskID: %d\n usage: %u.%u\n",
           taskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);

    /* 获取指定任务在系统启动到现在的CPU占用率 */
    cpupUse = LOS_HistoryTaskCpuUsage(taskID, CPUP_LESS_THAN_1S);
    printf("cpu usage of the cpupTestTask in all time:\n TaskID: %d\n usage: %u.%u\n",
           taskID, cpupUse / LOS_CPUP_PRECISION_MULT, cpupUse % LOS_CPUP_PRECISION_MULT);

    return LOS_OK;
}

SYS_RUN(cpup_entry);